'use strict';
/**
 * @fileoverview 玩家科技信息
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var mongoose = require('mongoose');
var techConf = require('../../config/techConf');
var techMgr = require('../../mgr/techMgr');

var techSchema = new mongoose.Schema({
    // 角色id
    role_id: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 已升级科技列表
    tech_list: {
        type: mongoose.Schema.Types.Mixed,
        default: {}
    },

    // 科技效果值
    tech_eff: {
        type: mongoose.Schema.Types.Mixed,
        default: {}
    },

    // 正在升级的科技
    up_tech_id: {
        type: Number,
        default: 0
    },

    // 科技升级起始时间戳
    up_tech_start_tm: {
        type: Number,
        default: 0
    },

    // 科技升级结束时间戳
    up_tech_end_tm: {
        type: Number,
        default: 0
    },

    // 科技战力
    tech_power: {
        type: Number,
        default: 0
    }
});

techSchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 推送到客户端
 * @return {[type]} [description]
 */
techSchema.methods.push = function () {
    this.pushModify();
};

/**
 * 推送修改
 * @return {[type]} [description]
 */
techSchema.methods.pushModify = function () {
    techMgr.sendTechChanged(
        this.role_id,
        this.getLastChange(),
        function (err, fails) {
            if (err) {
                console.error('send role message error: %j, fail ids: %j', err, fails);
                return;
            }
        }
    );
};

/**
 * 设置科技升级结束时间
 * @return {[type]} [description]
 */
techSchema.methods.setUpgradeTime = function (time) {
    this.up_tech_end_tm = time;
};

/**
 * 获取修改
 * @return {[type]} [description]
 */
techSchema.methods.getLastChange = function () {
    var tech_info = this.toJSON();
    tech_info.tech_eff = this.getTechEffList();
    return tech_info;
};

if (!techSchema.options.toJSON) {
    techSchema.options.toJSON = {};
}
/* jshint unused:false */
techSchema.options.toJSON.transform = function (doc, ret) {
    delete ret.tech_list;
    // delete ret.tech_eff;
    delete ret._id;
    delete ret.__v;
};

/**
 * 更新科技等级
 * @param  {[type]} tech_id [description]
 * @return {[type]}     [description]
 */
techSchema.methods.addTechLevel = function (tech_id) {
    var tech_lvl_cnf = techConf.getTechLvlConf(tech_id, 1);
    var tech_type = tech_lvl_cnf.tech_type;
    this.tech_list[tech_type] = this.tech_list[tech_type] || {};
    this.tech_list[tech_type][tech_id] = this.tech_list[tech_type][tech_id] || 0;
    this.tech_list[tech_type][tech_id]++;
    this.markModified('tech_list');

    this.saveEffByTechType(tech_type);
    return true;
};

/**
 * 取出玩家科技等级
 * @param  {[type]} tech_id [description]
 * @param  {[type]} tech_lv [description]
 * @return {[type]}     [description]
 */
techSchema.methods.getTechLvl = function (tech_id) {
    var tech_lvl_cnf = techConf.getTechLvlConf(tech_id, 1);
    if (!tech_lvl_cnf) {
        return 0;
    }

    var tech_type = tech_lvl_cnf.tech_type;
    if (!this.tech_list[tech_type]) {
        return 0;
    }
    return this.tech_list[tech_type][tech_id] || 0;
};

/**
 * 保存到科技升级队列
 * @param  {[type]} tech_id [description]
 * @param  {[type]} end_time [description]
 * @return {[type]}     [description]
 */
techSchema.methods.putToUpQue = function (tech_id, start_time, end_time) {
    if (this.up_tech_id !== 0) {
        return false;
    }

    this.up_tech_id = tech_id;
    this.up_tech_start_tm = start_time;
    this.up_tech_end_tm = end_time;
    return true;
};

/**
 * 按科技类型取出玩家相应科技列表
 * @param  {[type]} tech_type [description]
 * @return {[type]}     [description]
 */
techSchema.methods.getTechesByType = function (tech_type) {
    if (!this.tech_list[tech_type]) {
        return {};
    }
    return this.tech_list[tech_type];
};

/**
 * 按科技类型查询效果数值
 * @param  {[type]} tech_type [description]
 * @return {[type]}     [description]
 */
techSchema.methods.getEffByTechType = function (tech_type) {
    return this.tech_eff[tech_type] || 0;
};

/**
 * 查询科技效果列表
 * @param  {[type]} tech_type [description]
 * @return {[type]}     [description]
 */
techSchema.methods.getTechEffList = function () {
    var tech_eff_list = [];
    _.forEach(this.tech_eff, function (eff_value, tech_type) {
        tech_eff_list.push({
            tech_type: +tech_type,
            eff_value: eff_value
        });
    });
    return tech_eff_list;
};

/**
 * 按科技类型保存效果数值
 * @param  {[type]} tech_type [description]
 * @return {[type]}     [description]
 */
techSchema.methods.saveEffByTechType = function (tech_type) {
    if (!this.tech_list[tech_type]) {
        this.tech_eff[tech_type] = 0;
        this.markModified('tech_eff');
        return;
    }
    var eff_val = 0;
    var tech_list = this.tech_list[tech_type];
    for (var tech_id_key in tech_list) {
        var tech_id = parseInt(tech_id_key);
        var tech_lvl = tech_list[tech_id];
        eff_val += techConf.getTechEffValue(tech_id, tech_lvl);
    }
    this.tech_eff[tech_type] = eff_val;
    this.markModified('tech_eff');
};

mongoose.model('Tech', techSchema);
